#include "libs/duckdb/duckdb.h"
#include <stdio.h>
#include <stdlib.h>

int main() {
    duckdb_database db;
    duckdb_connection con;
    duckdb_result result;
    duckdb_state state;
    
    printf("Testing DuckDB C API...\n");
    
    // 打开内存数据库
    state = duckdb_open(":memory:", &db);
    if (state == DuckDBError) {
        printf("Failed to open database\n");
        return 1;
    }
    printf("✅ Database opened successfully\n");
    
    // 创建连接
    state = duckdb_connect(db, &con);
    if (state == DuckDBError) {
        printf("Failed to connect to database\n");
        duckdb_close(&db);
        return 1;
    }
    printf("✅ Connection established successfully\n");
    
    // 执行简单查询
    state = duckdb_query(con, "SELECT 42 as answer, 'Hello DuckDB' as message", &result);
    if (state == DuckDBError) {
        printf("Failed to execute query: %s\n", duckdb_result_error(&result));
        duckdb_destroy_result(&result);
        duckdb_disconnect(&con);
        duckdb_close(&db);
        return 1;
    }
    printf("✅ Query executed successfully\n");
    
    // 检查结果
    printf("Columns: %llu\n", duckdb_column_count(&result));
    printf("Column 0 name: %s, type: %d\n", duckdb_column_name(&result, 0), duckdb_column_type(&result, 0));
    printf("Column 1 name: %s, type: %d\n", duckdb_column_name(&result, 1), duckdb_column_type(&result, 1));
    
    // 清理资源
    duckdb_destroy_result(&result);
    duckdb_disconnect(&con);
    duckdb_close(&db);
    
    printf("✅ All tests passed! DuckDB is working correctly.\n");
    return 0;
}
